package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.spu.SpuPropertiesValues;
import com.hoshiicloud.goods.param.spu.SpuPropertiesValuesAddParam;
import com.hoshiicloud.goods.param.spu.SpuPropertiesValuesQueryParam;
import com.hoshiicloud.goods.param.spu.SpuPropertiesValuesUpdateParam;
import com.hoshiicloud.goods.service.SpuPropertiesValuesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品属性组 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/spu-properties-values")
@Api(value = "商品属性组", tags = {"商品属性组"})
public class SpuPropertiesValuesController {
    @Autowired
    private SpuPropertiesValuesService spuPropertiesValuesService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询商品属性组
     */
    @ApiOperation(value = "分页查询商品属性组")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid SpuPropertiesValuesQueryParam param) {
        try {
            log.info("分页查询商品属性组");
            QueryWrapper<SpuPropertiesValues> queryWrapper = new QueryWrapper<SpuPropertiesValues>();
            queryWrapper.lambda().eq(param.getSpuId() != null, SpuPropertiesValues::getSpuId, param.getSpuId())
                    .eq(param.getPropertyId() != null, SpuPropertiesValues::getPropertyId, param.getPropertyId())
                    .eq(param.getValue() != null, SpuPropertiesValues::getValue, param.getValue());
            IPage<SpuPropertiesValues> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "SpuPropertiesValuesQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            
            return Response.data(spuPropertiesValuesService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品属性组异常", e);
            return Response.failed("分页查询商品属性组异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询商品属性组
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品属性组")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(spuPropertiesValuesService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品属性组")
    public Response save(@RequestBody @Valid SpuPropertiesValuesAddParam param) {

        // Bean数据转换
        SpuPropertiesValues entity;
        try {
            entity = BeanUtils.covertType(param, SpuPropertiesValues.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(spuPropertiesValuesService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改商品属性组
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品属性组")
    public Response update(@RequestBody @Valid SpuPropertiesValuesUpdateParam param) {

        // Bean数据转换
        SpuPropertiesValues entity;
        try {
            entity = BeanUtils.covertType(param, SpuPropertiesValues.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(spuPropertiesValuesService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除商品属性组
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品属性组")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(spuPropertiesValuesService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询商品属性组")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(spuPropertiesValuesService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
